package edu.washington.vaccine.ui;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.DatePicker;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import edu.washington.vaccine.R;
import edu.washington.vaccine.backend.Vaccine;
import edu.washington.vaccine.backend.VaccineLocalHelper;

/*
 * 
 * Loads the Add Patient screen which permits the user to input
 * new data for a new patient. Once completed, calls SendDataListener
 * to manage storage.
 * 
 */
public class AddPatientActivity extends Activity {
	@SuppressWarnings("unused")
	private static final String TAG = "AddPatientActivity";
	private VaccineLocalHelper localDB;
	private boolean activeTable;
	private OnDateSetListener odsl;
	private int checkPointer;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		View viewToLoad = LayoutInflater.from(this.getParent()).inflate(R.layout.form, null);
	    this.setContentView(viewToLoad);
		//        View root = ((ViewGroup) findViewById(android.R.id.content)).getRootView();
		//        root.setFocusable(true);
		//        root.setFocusableInTouchMode(true);
		//        root.requestFocus();
		TextView title = (TextView) findViewById(R.id.title);
		title.setText(R.string.add_patient);
		
		checkPointer = 0;
		
		odsl=new OnDateSetListener() {
			public void onDateSet(DatePicker date, int year, int month, int dayOfMonth) {
				 TextView text = (TextView) findViewById(checkPointer);
				 String birthdate = "";
				 month++;
				 if (month < 10)
					 birthdate = "0";
				 birthdate += month + "-";

				 if (dayOfMonth < 10)
					 birthdate += "0";
				 birthdate += dayOfMonth + "-" + year;

//				 text.setText("    " + month + "-" + dayOfMonth + "-" + year);
				 text.setText(birthdate);
			}

		};
		final Calendar c = Calendar.getInstance();
		
		localDB = VaccineLocalHelper.getInstance(this);

		activeTable = false;
		final Button prevVaccine = (Button) findViewById(R.id.button1);
		prevVaccine.setTextSize(prevVaccine.getTextSize()-5);
		prevVaccine.setText("Enter Log Vaccines Already Recieved");

		prevVaccine.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if(!activeTable) {
					createTable();
					activeTable = true;
					prevVaccine.setText("Hide Selection");
				}
				else {
					LinearLayout l = (LinearLayout) findViewById(R.id.oldvaccine);
					ScrollView sv =  (ScrollView) findViewById(R.string.old_vaccine_listing);
					l.removeView(sv);
					activeTable = false;
					prevVaccine.setText("Enter Log Vaccines Already Recieved");
				}
			}
		});

		Button addpatientButton = (Button) findViewById(R.id.button2);
		addpatientButton.setText("Add Patient");
		addpatientButton.setOnClickListener(new SendDataListener(
		AddPatientActivity.this, true, PatientDataActivity.class, "patientdata"));

		DatePicker d = (DatePicker) findViewById(R.id.birthdate);
		d.init(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE), DateLimiter.getInstance());

	}

	//creates an expandable table of checkable boxes each labeled
	//with a vaccine. Once selected the user inputs a date via a date
	//dialog, once set the date is placed next to the check.
	private void createTable() {
		final List<Vaccine> pArray = new ArrayList<Vaccine>();
		LinearLayout l = (LinearLayout) findViewById(R.id.oldvaccine);
		Cursor cursor = localDB.getReadableDatabase().rawQuery("SELECT * FROM Vaccines", new String [] {});
		startManagingCursor(cursor);
		// collect data from local database 
		ScrollView sv = new ScrollView(this);
		sv.setId(R.string.old_vaccine_listing);
		TableLayout tl = new TableLayout(this);
		sv.addView(tl);
		while (cursor.moveToNext()) {
			int vid = cursor.getInt(0);
			String vname = cursor.getString(1);
			String vdesc = cursor.getString(2);
			String vschedule = cursor.getString(3);
			pArray.add(new Vaccine(vid, vname, vdesc, vschedule));
		}
		cursor.close();

		for( Vaccine v : pArray) {
			TableRow tr = new TableRow(this);
			CheckBox chk = new CheckBox(this);
			final TextView date = new TextView(this);
			chk.setTextColor(R.color.black);
			chk.setText(v.name);
			date.setId(v.id);
			chk.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					checkPointer = date.getId();
					if(isChecked) {
						Calendar cal=Calendar.getInstance();
						DatePickerDialog datePickDiag = new DatePickerDialog(getParent().getParent(),
								odsl,cal.get(Calendar.YEAR),
								cal.get(Calendar.MONTH),
								cal.get(Calendar.DAY_OF_MONTH));
						datePickDiag.show();
					}
					else {
						TextView text = (TextView) findViewById(checkPointer);
						text.setText("");
					}
				}
			});
			tr.addView(chk);
			tr.addView(date);
			tl.addView(tr);
		}
		l.addView(sv);
	}

	@Override
	public void onBackPressed() {
		//		Intent i = new Intent(this, VaccineActivity.class);
		//		VaccineActivity parentActivity = (VaccineActivity)getParent();
		//		parentActivity.replaceContentView("home", i);

		//    	VaccineActivity parentActivity = (VaccineActivity)getParent();
		//    	parentActivity.onBackPressed();
		FirstGroup.group.back();
	}
}